package com.dfrz.bookstore.util;

import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.UserService;
import com.dfrz.bookstore.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

/**
 * 类功能描述:
 *      导入数据
 * @Author keyan
 * @Date 2021/5/30 1:57
 */
public class InputDataUtile {

    public static void main(String[] args) {
        File file = new File("TestIO/BookStore/userInfo.xls");
        inPutUserInfo(file);
    }

    /**
     * 导入用户信息
     * @param
     */
    public static void inPutUserInfo(File file){
        FileInputStream fin = null;
        UserServiceImpl userService = new UserServiceImpl();
        try {
            fin  = new FileInputStream(file);
            HSSFWorkbook wb = new HSSFWorkbook(fin);//将输出流加载到xls 工作对象中
            HSSFSheet sheet = wb.getSheetAt(0); //获取 表格页码
            if (sheet!=null){  //判断是否存在
                Iterator<Row> rows = sheet.iterator();//获得所有行
                rows.next();//先让迭代器调过一行(这行是标题)
                int i = 0;//计数
                while (rows.hasNext()){
                    Row row = rows.next();
                    User user = new User(); //ID自动增长 不需要赋值
                    user.setLoginName(row.getCell(1).toString());
                    List<User> userList = userService.findUserByUser(user);
                    if (userList.size()>0){
                        //如果登录名重复 就跳过
                        JOptionPane.showMessageDialog(null,user.getLoginName()+"已存在,已过滤");
                        continue;
                    }
                    user.setPassWord(row.getCell(2).toString());
                    user.setName(row.getCell(3).toString());
                    user.setSex(row.getCell(4).toString());
                    user.setEmail(row.getCell(5).toString());
                    user.setTel(row.getCell(6).toString());
                    String s1 = row.getCell(7).toString();
                    String s2 = StringUtils.substringBefore(s1, ".");
                    user.setLevel(Integer.valueOf(s2));
                    user.setAddress(row.getCell(8).toString());
                    user.setUserImage(row.getCell(9).toString());
                    boolean flag = userService.insertUser(user);
                    if (flag){
                        i++; //计数
                    }
                }
                JOptionPane.showMessageDialog(null,"成功导入"+i+"位用户信息");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fin!=null){
                    fin.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void inPutBookInfo(File f) {
        System.out.println("和批量导入用户一样");
    }
}
